package com.goods.business.service.imp;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.OutStockInfoMapper;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.*;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OutStockServiceImpl implements OutStockService {


    @Resource
    private OutStockMapper outStockMapper;
    @Resource
    private ConsumerMapper consumerMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private OutStockInfoMapper outStockInfoMapper;


    @Override
    public Map findOutStockList(Integer pageNum, Integer pageSize, Integer status, Integer type, String outNum) {
        Example example = new Example(OutStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
        if (!StringUtils.isEmpty(type)){
            criteria.andEqualTo("type",type);
        }
        if (!StringUtils.isEmpty(outNum)){
            criteria.andEqualTo("outNum",outNum);
        }
        List<OutStock> outStockList = outStockMapper.selectByExample(example);

        //转Vo
        List<OutStockVO> outStockVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(outStockList)){
            for (OutStock outStock : outStockList) {
                Long consumerId = outStock.getConsumerId();
                Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
                OutStockVO outStockVO = new OutStockVO();
                BeanUtils.copyProperties(outStock,outStockVO);
                outStockVO.setPhone(consumer.getPhone());
                outStockVOList.add(outStockVO);
            }
        }
        //分页
        List<OutStockVO> page = ListPageUtils.page(outStockVOList, pageSize, pageNum);
        int total = outStockVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);
        return map;
    }

    @Override
    public OutStockDetailVO detail(Long id) {
        OutStockDetailVO outStockDetailVO = new OutStockDetailVO();
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        String outNum = outStock.getOutNum();
        List<OutStockInfo> outStockInfoList = outStockInfoMapper.selectByOutNum(outNum);
        //查询product封装成OutStockItemVO集合
        List<OutStockItemVO> outStockItemVOList = new ArrayList<>();
        for (OutStockInfo outStockInfo : outStockInfoList) {
            OutStockItemVO outStockItemVO = new OutStockItemVO();
            String pNum = outStockInfo.getPNum();
            Product product = productMapper.selectBypNum(pNum);
            BeanUtils.copyProperties(product,outStockItemVO);
            outStockItemVO.setCount(outStockInfo.getProductNumber());
            outStockItemVOList.add(outStockItemVO);
        }


        //查询ConsumerVO
        Long consumerId = outStock.getConsumerId();
        Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);

        //封装outStockDetailVO对象

        outStockDetailVO.setOutNum(outStock.getOutNum());
        outStockDetailVO.setStatus(outStock.getStatus());
        outStockDetailVO.setType(outStock.getType());
        outStockDetailVO.setOperator(outStock.getOperator());
        outStockDetailVO.setConsumerVO(consumerVO);
        outStockDetailVO.setTotal(outStockItemVOList.size());
        outStockDetailVO.setItemVOS(outStockItemVOList);

        return outStockDetailVO;
    }

    @Override
    public int remove(Long id) {
        int status = 1;
        int i = outStockMapper.updateByStatus(status,id);
        return i;
    }

    @Override
    public int back(Long id) {
        int status = 0;
        int i = outStockMapper.updateByStatus(status,id);
        return i;
    }

    @Override
    public int delete(Long id) {
        int i = outStockMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public void addIntoStock(Map map) {
        OutStock outStock = new OutStock();
        Consumer consumer = new Consumer();
        String uuid = UUID.randomUUID().toString();
        int productNum = 0;
        outStock.setOutNum(uuid);
        if (map.get("consumerId") == null){
            consumer.setAddress((String) map.get("address"));
            consumer.setName((String) map.get("name"));
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumer.setPhone((String) map.get("phone"));
            consumer.setSort((Integer) map.get("sort"));
            consumer.setContact((String) map.get("contact"));
            consumerMapper.insert(consumer);
            outStock.setConsumerId(consumer.getId());
        }else {
            Long consumerId = Long.valueOf(String.valueOf(map.get("consumerId")));
            outStock.setConsumerId(consumerId);
        }
        List<Map> productList = (List<Map>) map.get("products");
        for (Map product : productList) {
            OutStockInfo outStockInfo = new OutStockInfo();
            Object productId = product.get("productId");
            Product product1 = productMapper.selectByPrimaryKey(productId);
            outStockInfo.setOutNum(uuid);
            outStockInfo.setPNum(product1.getPNum());
            outStockInfo.setProductNumber((Integer) product.get("productNumber"));
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfoMapper.insert(outStockInfo);
            productNum+=outStockInfo.getProductNumber();
        }
        outStock.setType((Integer) map.get("type"));
        outStock.setOperator("admin");
        outStock.setCreateTime(new Date());
        outStock.setProductNumber(productNum);
        outStock.setRemark((String) map.get("remark"));
        outStock.setStatus(2);
        outStock.setPriority((Integer) map.get("priority"));
        outStockMapper.insert(outStock);
    }

    @Override
    public int publish(Long id) {
        int status = 0;
        int i = outStockMapper.updateByStatus(status,id);
        return i;
    }
}
